package iosr11.gbase.dao;

import iosr11.data.Event;
import iosr11.data.Note;
import iosr11.data.ProjectGroup;

import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.gdata.data.PlainTextConstruct;

public class NoteDAOImpl extends GenericDAOImpl<Note> implements NoteDAO{

	private static String TYPE = "System notes";
	private static String TEXT = "text";
	private static String GROUP_REF = "project group";
	private static String EVENT_REF = "event";
	
	
	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public Note rewrite(GoogleBaseEntry entry) {
		Note entity = new Note();
		entity.setId(entry.getId());
		entity.setText(entry.getGoogleBaseAttributes().getTextAttribute(TEXT));
		Event event = new Event();
		event.setId(entry.getGoogleBaseAttributes().getTextAttribute(EVENT_REF));
		entity.setEvent(event);
		ProjectGroup group = new ProjectGroup();
		group.setId(entry.getGoogleBaseAttributes().getTextAttribute(GROUP_REF));
		entity.setGroup(group);
		
		return entity;
	}

	@Override
	public GoogleBaseEntry rewrite(Note entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		
		entry.getGoogleBaseAttributes().addTextAttribute(GROUP_REF, entity.getGroup().getId());
		entry.getGoogleBaseAttributes().addTextAttribute(EVENT_REF, entity.getEvent().getId());
		entry.getGoogleBaseAttributes().addTextAttribute(TEXT, entity.getText());
		
		return entry;
	}

}
